home *** CD-ROM | disk | FTP | other *** search
- Path: news.halcyon.com!usenet
- From: normanb@halcyon.com (Norm Bryar)
- Newsgroups: comp.lang.c++
- Subject: Re: Output to file vs. cout
- Date: Sun, 10 Mar 1996 17:27:20 GMT
- Organization: Northwest Nexus Inc.
- Message-ID: <4hv3cf$e93@news.halcyon.com>
- References: <JL.96Mar7165152@thyme.id.dth.dk>
- NNTP-Posting-Host: blv-pm10-ip17.halcyon.com
- X-Newsreader: Forte Free Agent 1.0.82
-
- jl@id.dth.dk (J°rn Lind-Nielsen) wrote:
-
-
- >Here's another of the probs that comes from porting C to C++:
-
- >- I want to be able to redirect program output to either a file or cout.
- > Typically this would be selected by a commandline option ("-o outfile").
-
- >- In C I would do something like this:
-
-
- > main()
- > {
- > FILE *outputfile;
-
- > if (commandline == do_output_to_file)
- > outputfile = stdout;
- > else
- > outputfile = fopen(passed_filename, "w");
-
- > fprintf(outfile, "Hello world\n");
-
- > fclose(outfile); /* Maybe - not allways necassary */
- > }
-
-
-
- >- The question is: How is this done in C++ ?
-
- Of course, it could be done exactly the same way.
-
- The iostream classes, btw, don't particularly care what the output or
- input device really is. cout and cin are just streams that already
- point to the console. You can make file or memory archives that
- conform to the ostream and istream interfaces and substitute them at
- will.
-
- archive << "My name: " << szName << "\n"; // etc.
-
- --Norm
-
-